﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Job.Core
{
    public class KeywordHelper
    {
        /// <summary>
        /// 高亮显示关键字
        /// </summary>
        /// <param name="str"></param>
        /// <param name="keyword"></param>
        /// <returns></returns>
        public static string ReplaceKeyword(string str, string keyword)
        {
            if (string.IsNullOrEmpty(str))
            { return string.Empty; }
            return Regex.Replace(str, "(>([^<>]|\\n)*)(" + keyword + ")(([^<>]|\\n)*</)", "$1<a style=\"background-color:yellow;color:red\" name=\"$3\">$3</a>$4", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 处理关键字 移除 特殊、空格
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string DisposeKeywords(string strKey)
        {
            strKey = strKey.Replace(";", " ");
            strKey = strKey.Replace("\"", " ");
            strKey = strKey.Replace(">", " ");
            strKey = strKey.Replace("<", " ");
            strKey = strKey.Replace("(", " ");
            strKey = strKey.Replace(")", " ");
            strKey = strKey.Replace("?", " ");
            strKey = strKey.Replace("@", " ");
            strKey = strKey.Replace("*", " ");
            strKey = strKey.Replace("{", " ");
            strKey = strKey.Replace("}", " ");
            strKey = strKey.Replace("[", " ");
            strKey = strKey.Replace("]", " ");
            strKey = strKey.Replace("#", " ");
            strKey = strKey.Replace("~", " ");
            strKey = strKey.Replace("!", " ");
            strKey = strKey.Replace(":", " ");
            strKey = strKey.Replace("|", " ");
            strKey = strKey.Replace("，", ",");
            strKey = strKey.Replace("=", " ");
            strKey = strKey.Replace("^", " ");
            strKey = strKey.Replace("$", " ");
            strKey = strKey.Replace("\\", " ");
            strKey = strKey.Trim();
            strKey = CRegex.Replace(strKey, @"\s+", " ", 0);
            return strKey;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string DisposeSearchKeywords(string strKey)
        {
            return strKey.Replace("\\", "\\\\").Replace("(", "\\(").Replace(")", "\\)").Replace("[", "\\[").Replace("]", "\\]").Replace("{", "\\{").Replace("}", "\\}").Replace("^", "\\^").Replace("~", "\\~").Replace(":", "\\:").Replace("?", "\\?").Replace("*", "\\*").Replace("!", "\\!").Replace("\"", "\\\"").Replace("<", "\\<").Replace(">", "\\>").Replace("_", "\\_");
        }

        /// <summary>
        /// 处理URL
        /// </summary>
        /// <param name="strKey"></param>
        /// <returns></returns>
        public static string DisposeUrlWords(string strKey)
        {
            strKey = strKey.Replace("&quot;", " ");
            strKey = strKey.Replace(";", " ");
            strKey = strKey.Replace("&", " ");
            strKey = strKey.Replace("\"", " ");
            strKey = strKey.Replace(">", " ");
            strKey = strKey.Replace("<", " ");
            strKey = strKey.Replace("(", " ");
            strKey = strKey.Replace(")", " ");
            strKey = strKey.Replace("?", " ");
            strKey = strKey.Replace("@", " ");
            strKey = strKey.Replace("*", " ");
            strKey = strKey.Replace("{", " ");
            strKey = strKey.Replace("}", " ");
            strKey = strKey.Replace("[", " ");
            strKey = strKey.Replace("]", " ");
            strKey = strKey.Replace("#", " ");
            strKey = strKey.Replace("~", " ");
            strKey = strKey.Replace("!", " ");
            strKey = strKey.Replace(":", " ");
            strKey = strKey.Replace("|", " ");
            strKey = strKey.Replace("，", ",");
            strKey = strKey.Replace(",", " ");
            strKey = strKey.Replace("=", " ");
            strKey = strKey.Replace("^", " ");
            strKey = strKey.Replace("$", " ");
            strKey = strKey.Replace("%", " ");
            strKey = strKey.Replace("\\", " ");
            strKey = strKey.Replace("'", "");
            strKey = strKey.Replace(".", "");
            strKey = strKey.Trim();
            strKey = CRegex.Replace(strKey, @"\s+", " ", 0);
            strKey = CRegex.Replace(strKey, @"\s+", "_", 0);
            return strKey;
        }


        /// <summary>
        /// 用于获取在原图片基础上扩展来的图片名称
        /// </summary>
        /// <param name="strUrl"></param>
        /// <returns></returns>
        public static string[] GetPicUrlByDelete(string strUrl)
        {
            string[] strArr = new string[5] { "", "", "", "", "" };

            if (strUrl.IndexOf(".") != -1)
            {
                int i = strUrl.LastIndexOf(".");
                string strTemp1 = strUrl.Substring(0, i);
                string strTemp2 = strUrl.Substring(i, strUrl.Length - i);

                strArr[0] = string.Format("{0}1{1}", strTemp1, strTemp2);//50*50
                strArr[1] = string.Format("{0}2{1}", strTemp1, strTemp2);//100*100
                strArr[2] = string.Format("{0}3{1}", strTemp1, strTemp2);//220*200
                strArr[3] = string.Format("{0}4{1}", strTemp1, strTemp2);//320*240
                strArr[4] = string.Format("{0}5{1}", strTemp1, strTemp2);//120*90(sampledeal) | 75*75(product)
            }
            return strArr;
        }

        /// <summary>
        /// 得到图片相对路径 带"/"
        /// </summary>
        /// <param name="strImgUrl"></param>
        /// <returns></returns>
        public static string replaceImgURL(string strImgUrl)
        {
            strImgUrl = strImgUrl.ToLower();

            if (!string.IsNullOrEmpty(strImgUrl))
            {
                int iIndex = strImgUrl.IndexOf("picture");
                if (iIndex == -1)
                    strImgUrl = "/images/small_default.JPG";
                else
                    strImgUrl = "/" + strImgUrl.Substring(iIndex, strImgUrl.Length - iIndex).Replace("\\", "/").Replace("&", "");
            }
            else
                strImgUrl = "/images/small_default.JPG";
            return strImgUrl;
        }

        public static string GB2312ToUTF8(string str)
        {
            try
            {
                Encoding uft8 = Encoding.GetEncoding(65001);
                Encoding gb2312 = Encoding.GetEncoding("gb2312");

                byte[] temp = gb2312.GetBytes(str);

                //Response.Write("gb2312的编码的字节个数：" + temp.Length+"<br>");

                //for (int i = 0; i < temp.Length; i++)
                //{
                //    Response.Write(Convert.ToUInt16(temp[i]).ToString() + "<br>");
                //}

                byte[] temp1 = Encoding.Convert(gb2312, uft8, temp);

                //Response.Write("uft8的编码的字节个数：" + temp1.Length + "<br>");

                //for (int i = 0; i < temp1.Length; i++)
                //{

                //    Response.Write(Convert.ToUInt16(temp1[i]).ToString() + "<br>");

                //}

                return uft8.GetString(temp1);
            }
            catch
            {
                //Response.Write(ex.ToString());
                return "";
            }
        }

        /// <summary>
        /// 把一个字符串中的 低序位 ASCII 字符 替换成 &#x  字符
        /// 转换  ASCII  0 - 8  -> &#x0 - &#x8
        /// 转换  ASCII 11 - 12 -> &#xB - &#xC
        /// 转换  ASCII 14 - 31 -> &#xE - &#x1F
        /// </summary>
        /// <param name="tmp"></param>
        /// <returns></returns>
        public static string ReplaceLowOrderASCIICharacters(string tmp)
        {
            StringBuilder info = new StringBuilder();
            foreach (char cc in tmp)
            {
                int ss = (int)cc;
                if (((ss >= 0) && (ss <= 8)) || ((ss >= 11) && (ss <= 12)) || ((ss >= 14) && (ss <= 32)))
                    info.AppendFormat("&#x{0:X};", ss);
                else info.Append(cc);
            }
            return info.ToString().Replace("&#x20;", " ");
        }

    }
}
